<?php 
/**
 * 购物车控制器
 */
class ShopController extends Controller{
    /**
     * 首页
     */  
    public function index(){
    	if(IS_POST){
    		$gid = $_POST['gid'];
			$Ginfo = K('Goods')->where("gid={$gid}")->find();
    		$data = array(
				'id' => $_POST['gid'],
				'name' => $_POST['gname'],
				'num' => $_POST['num'],
				'price' => $_POST['specid'],
				"options"=>array(
			         "img"=> $Ginfo['img'],
			         "marketprice" =>$Ginfo['marketprice'],
			         "specName" =>$_POST['specName']
			       )
			);
			Cart::add($data);
			$this->success('加入购物车成功',U('index'));
    	}
		//如果购物车内有商品
		if(isset($_SESSION['cart'])){
			$Gdata = $_SESSION['cart'];
			$this->assign('Gdata',$Gdata);
		}
		//*******************************************************************
		//今日商品处理
		$todayData = K('Goods')->order("addtime")->all();
		$this->assign('todayData',$todayData);
		
		
		
		
		
        $this->display(); 
    }
	
	/**
	 * 商品数量增减
	 */
	public function AddDel(){
	    if(IS_AJAX){
			$sid = Q('post.sid');
			$num = Q('post.num',0,'intval');
			$data = array(
			'sid'=>$sid,// 唯一 sid，添加购物车时自动生成
			'num'=>$num
		);
		$this->UpCart($data);
		$infoNum = $_SESSION['cart']['total_rows'];
		$infoTotalPrice = $_SESSION['cart']['total'];
		$info = $_SESSION['cart']['goods'][$sid];
		$info['TotalPrice'] =$infoTotalPrice;
		$info['goodsNum'] = $infoNum;
		echo json_encode($info);
		exit;
		}
	}
	
	/**
	 * 删除商品
	 */
	public function delGoods(){
	    $sid = Q('get.sid');
		Cart::del($sid);
		$this->success('删除成功');
	}
	
	
	/**
	 * 购物车更新
	 */
	public function UpCart($data){
		Cart::update($data);
	}
	
	
	
 }
 
 
 

 
 
 
 
 




 ?>